package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.Exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //判断房间id是否为null
        boolean isNULL = roomSubmitVo.getId() != null;

        //房间的saveOrUpdate
        saveOrUpdate(roomSubmitVo);

        //其他关系表的删除
        if (isNULL) {
            //图片
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapperGI = new LambdaQueryWrapper<>();
            lambdaQueryWrapperGI.eq(GraphInfo::getId, roomSubmitVo.getId());
            lambdaQueryWrapperGI.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(lambdaQueryWrapperGI);
            //属性值
            LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapperAV = new LambdaQueryWrapper<>();
            lambdaQueryWrapperAV.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(lambdaQueryWrapperAV);
            //配套
            LambdaQueryWrapper<RoomFacility> lambdaQueryWrapperRF = new LambdaQueryWrapper<>();
            lambdaQueryWrapperRF.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(lambdaQueryWrapperRF);
            //标签
            LambdaQueryWrapper<RoomLabel> lambdaQueryWrapperRL = new LambdaQueryWrapper<>();
            lambdaQueryWrapperRL.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(lambdaQueryWrapperRL);
            //支付方式
            LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapperPT = new LambdaQueryWrapper<>();
            lambdaQueryWrapperPT.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(lambdaQueryWrapperPT);
            //租期
            LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapperRLT = new LambdaQueryWrapper<>();
            lambdaQueryWrapperRLT.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(lambdaQueryWrapperRLT);
        }
        //其他关系表的添加
        //图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //属性值
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue build = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValues.add(build);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabels = new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabels.add(build);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public void customPage(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        roomInfoMapper.pageRoomItemByQuery(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {

        //查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        //查询所有公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //图片--和公寓的图片共用接口
        List<GraphVo> graphVos = graphInfoMapper.customGet(id, ItemType.ROOM);

        //属性值
        List<AttrValueVo> attrValueVoList = attrValueMapper.customGetAttrValueByRoomId(id);

        //配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.customGetByRoomID(id);

        //标签
        List<LabelInfo> labelInfoList = labelInfoMapper.customGetByRoomId(id);
        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.customGetBYRoomId(id);

        //租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.customGetByRoomId(id);
        //创建RoomDetailVo
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //添加其他详情信息
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

        //判断房间是否在租，否则抛出异常
        int count = roomInfoMapper.existsLeaseAgreementByRoomId(id);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR.getCode(),
                    ResultCodeEnum.DELETE_ERROR.getMessage());
        }
        //删除roomByID
        removeById(id);
        //删除图片
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapperGI = new LambdaQueryWrapper<>();
        lambdaQueryWrapperGI.eq(GraphInfo::getItemType, ItemType.ROOM);
        lambdaQueryWrapperGI.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(lambdaQueryWrapperGI);

        //删除删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapperRA = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRA.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(lambdaQueryWrapperRA);
        //删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> lambdaQueryWrapperRF = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRF.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(lambdaQueryWrapperRF);
        //删除labelInfoList
        LambdaQueryWrapper<RoomLabel> lambdaQueryWrapperRL = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRL.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(lambdaQueryWrapperRL);
        //删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapperRPT = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRPT.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(lambdaQueryWrapperRPT);
        //删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapperRLT = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRLT.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(lambdaQueryWrapperRLT);
    }
}




